<template>
    <div>
        <el-button type="success" @click="dialogFormVisible = true">Buy now</el-button>
        <el-dialog title="订单信息" :visible.sync="dialogFormVisible">
            <v-address ref="address_list" v-show="!new_info"/>
            <el-form :model="form" v-show="new_info">
                <el-form-item label="Accepter" :label-width="formLabelWidth">
                <el-input v-model="form.accepter" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="ShipCountry" :label-width="formLabelWidth">
                <el-input v-model="form.shipcountry" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="ShipCity" :label-width="formLabelWidth">
                <el-input v-model="form.shipcity" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="DetailAddress" :label-width="formLabelWidth">
                <el-input v-model="form.detailaddress" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="new_info = !new_info">{{new_info?'返 回':'新 增'}}</el-button>
                <el-button type="primary" @click="buy">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import address from '../cart/address.vue'
import { baseURL } from '../../config'
export default {
    name: 'buyGoods',
    components:{
        'v-address': address
    },
    props:{
        fromItem: [Boolean],
        tableData: {
            type: Array,
            default: function(){
                return new Array();
            } 
        },
        item: {
            type: Object,
            default: function(){
                return {};
            }
        }
    },
    data () {
        return {
            dialogFormVisible: false,
            formLabelWidth: '120px',
            new_info: false,
            form :{
                accepter: '',
                shipcountry: '',
                shipcity: '',
                detailaddress: '',
            }
        }
    },
    mounted(){
        this.form.accepter = sessionStorage.getItem("lastname");
        this.form.shipcountry = sessionStorage.getItem("country");
        this.form.shipcity = sessionStorage.getItem("city");
        this.form.detailaddress = sessionStorage.getItem("addr1");
    },
    methods:{
        buyForOldAddress(){
            let obj = null;
            let address_list = this.$refs.address_list;
            address_list.address_list.forEach(e=>{
                if (e.addressid === address_list.direction){
                    obj = e;
                }
            });
            return obj;
        },
        buy(){
            this.dialogFormVisible = false;
            this.fromItem?this.buyFromItem():this.buyFromCarts();
        },
        addAddress(args){
            let args2 = Object.assign({},args)
            delete args2.cart_list
            this.$Axios({
                method: 'post',
                url: '/account/add-address',
                data: args2
            }).then(res => {
                console.log(res.data);
            }).catch(error=>{
                this.$message.error("其他服务器"+error);
            })
        },
        getAddress(args){
            if (!this.new_info){
                let obj = this.buyForOldAddress();
                args.accepter = obj.shiptolastname;
                args.shipcountry = obj.shipcountry;
                args.shipcity = obj.shipcity;
                args.detailaddress = obj.shipaddr1;
            }
            else { 
                args.accepter = this.form.accepter;
                args.shipcountry = this.form.shipcountry;
                args.shipcity = this.form.shipcity;
                args.detailaddress = this.form.detailaddress;
                this.addAddress(args);
            }
        },
        buyFromCarts(){
            let cart_list = [];
            this.tableData.forEach(e=>{
            if(e.checked == true){
                cart_list.push(e.cartid);
                }
            })
            if (cart_list.length == 0){
                this.$message.warning("您还未选择任何物品");
                return;
            }
            let args = {
                cart_list: cart_list,
            };
            this.getAddress(args);
            this.$Axios({ 
                method: 'post',
                url: '/order/add-orders',
                data: args
            }).then(res=>{
                location.href = baseURL + '/pay?totalprice='+res.data.data;
            }).catch(error=>{
                this.$message.error("其他服务器错误"+error);
            });
        },
        buyFromItem(){
            let args = {};
            args.item = this.item;
            this.getAddress(args);
            this.$Axios({
                method: 'post',
                url: 'order/add-order',
                data: args
            }).then(res=>{
                location.href = baseURL + '/pay?totalprice='+this.item.listprice;
            }).catch(error=>{
                this.$message.error("其他服务器错误"+error);
            })
        }
    }
}
</script>